<?php
	include("DBConn.php");
	$item_name = $_POST['item_name'];
?>
<html>
<body>
<?php
	$link = connectToDB();
	$strquery = "select * from added_items where ITEM_NAME='$item_name' order by DATE_ADDED desc;";
	$result = mysql_query($strquery) or die(mysql_error());
	
	$rowCount = mysql_num_rows($result);
	if($rowCount==0){
		echo "<p/><center><img src='sadpedobear.jpg' width=20% height=50%></center>";
		echo "<h1><center>Item not found!</center></h1>";
	}
	else{
		echo "<p/><center><img src='happypedobear2.bmp' width=50% height=75%></center>";
		?>
	<HR WIDTH="90%" SIZE="2" NOSHADE>
		<p/>
		<center>
		<fieldset>
		<legend>Adding of Items</legend>
		<table border="1" cellspacing="0" width="80%">
			<tr>
			<?php
			
			while ($field=mysql_fetch_field($result)) {
			echo "<th>";
			echo "<center>$field->name</center>";
			echo "</th>";
			}
			echo "</tr>";

			while ($row = mysql_fetch_row($result)) {
			echo "<tr>";
			for ($i=0; $i<mysql_num_fields($result); $i++) {
			echo "<td>";
			if($row[$i]!='0000-00-00 00:00:00')
				echo "<center>$row[$i]</center>";
			echo "</td>";
			}
			echo "</tr>\n";
			}

		echo "</table></fieldset></center>";
		
		
		$strquery = "select * from items_taken where ITEM_NAME='$item_name' order by DATE_TAKEN desc;";
	$result = mysql_query($strquery) or die(mysql_error());
	
	?>
		<p/>
		<center>
		<fieldset>
		<legend>Taking of Items</legend>
		<?php
			$rowCount=mysql_num_rows($result); 
			if($rowCount==0){
				echo "<h2>No taking of this item was recorded.</h2></fieldset>";
			}
			else {
			?>
				<table border="1" cellspacing="0" width="80%">
					<tr>
					<?php
					
					while ($field=mysql_fetch_field($result)) {
					echo "<th>";
					echo "<center>$field->name</center>";
					echo "</th>";
					}
					echo "</tr>";

					while ($row = mysql_fetch_row($result)) {
					echo "<tr>";
					for ($i=0; $i<mysql_num_fields($result); $i++) {
					echo "<td>";
					if($row[$i]!='0000-00-00 00:00:00')
						echo "<center>$row[$i]</center>";
					echo "</td>";
					}
					echo "</tr>\n";
					}

				echo "</table></center></fieldset>";
			}
	
	$strquery = "select * from item_status where ITEM_NAME='$item_name';";
	$result = mysql_query($strquery) or die(mysql_error());
	
	?>
		<center>
		<p/>
		<fieldset>
		<legend>Item Status</legend>
		<table border="1" cellspacing="0" width="80%">
			<tr>
			<?php
			
			while ($field=mysql_fetch_field($result)) {
			echo "<th>";
			echo "<center>$field->name</center>";
			echo "</th>";
			}
			echo "</tr>";

			while ($row = mysql_fetch_row($result)) {
			echo "<tr>";
			for ($i=0; $i<mysql_num_fields($result); $i++) {
			echo "<td>";
			if($row[$i]!='0000-00-00 00:00:00')
				echo "<center>$row[$i]</center>";
			echo "</td>";
			}
			echo "</tr>\n";
			}

		echo "</table></center></fieldset>";
	
	
	$strqueryP = "select sum(QUANTITY) from items_taken where ITEM_NAME='$item_name' and PURPOSE='Pre School';";
	$resultP = mysql_query($strqueryP) or die(mysql_error());
	$rowP = mysql_fetch_row($resultP);
	
	$strqueryE = "select sum(QUANTITY) from items_taken where ITEM_NAME='$item_name' and PURPOSE='Elementary';";
	$resultE = mysql_query($strqueryE) or die(mysql_error());
	$rowE = mysql_fetch_row($resultE);
	
	$strqueryH = "select sum(QUANTITY) from items_taken where ITEM_NAME='$item_name' and PURPOSE='High School';";
	$resultH = mysql_query($strqueryH) or die(mysql_error());
	$rowH = mysql_fetch_row($resultH);
	
	$strqueryG = "select sum(QUANTITY) from items_taken where ITEM_NAME='$item_name' and PURPOSE='General';";
	$resultG = mysql_query($strqueryG) or die(mysql_error());
	$rowG = mysql_fetch_row($resultG);
	
		
?>

		<center>
		<p/>
		<fieldset style="width:200px">
		<legend>Totals</legend>
		<table border="0" cellspacing="1" width="40%">
			<tr>
			<?php
			
			if($rowP[0]=="") $rowP[0]=0;
			if($rowE[0]=="") $rowE[0]=0;
			if($rowH[0]=="") $rowH[0]=0;
			if($rowG[0]=="") $rowG[0]=0;
			
			echo "<tr><td>";
			echo "Pre School : <td>$rowP[0]</td>";
			echo "</tr></td>";
			echo "<tr><td>";
			echo "Elementary : <td>$rowE[0]</td>";
			echo "</tr></td>";			
			echo "<tr><td>";
			echo "High School : <td>$rowH[0]</td>";
			echo "</tr></td>";
			ECHO "<tr><td>";
			echo "General : <td>$rowH[0]</td>";
			echo "</tr></td>";

			echo "</tr></table></fieldset></center>";
		mysql_close($link);
	}
?>
	
</body>
</html>